# -*- coding: utf-8 -*-
import scrapy
from ..items import mtItem


class MeituanSpider(scrapy.Spider):
    name = 'meituan'
    allowed_domains = ['meituan.com']
    start_urls = ['https://sy.meituan.com/yundongjianshen/pn1']

    def parse(self, response):
        datas = response.xpath('//div[@class="common-list-main"]/div')
        for data in datas:
            detail_url = data.xpath('./a/@href').get()
            yield scrapy.Request('https:' + detail_url, callback=self.detail_parse)
        next_link = response.xpath("//li[contains(@class, 'next-btn')]/a/@href").get()
        if next_link is not None:
            yield scrapy.Request('https:' + next_link, callback=self.parse)

    def detail_parse(self, response):
        item = mtItem()
        item['shop_name'] = response.xpath("//h1[@class='seller-name']/text()").get()
        item['shop_addr'] = response.xpath("string(//div[@class='item']/a[1])").get()
        item['tel'] = response.xpath("//div[@class='item'][2]/span[2]/text()").get()
        yield item
